package com.water.wfo.component.api.domain;

import java.util.Random;

/**
 * packageName com.water.wfo.component.api.domain
 *
 * @author
 * @className Token(处以class为网)
 * @date 2025/1/17
 * @description T0D0
 */
public class Token {
    private static Random random = new Random(System.currentTimeMillis());
    private String userId;
    private String sessionId;
    private String applicationId;
    private String uuid;
    private Long expireTime = System.currentTimeMillis();

    public Token(String userId, String applicationId, String uuid) {
        this.userId = userId;
        this.applicationId = applicationId;
        this.uuid = uuid;
    }

    public Token(String userId, String sessionId, String applicationId, String uuid, Long expireTime) {
        this.userId = userId;
        this.sessionId = sessionId;
        this.applicationId = applicationId;
        this.uuid = uuid;
        this.expireTime = expireTime;
    }

    public Token() {
    }

    public String getTokenKey() {
        return this.userId + this.sessionId + this.applicationId + this.uuid;
    }
    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public String getApplicationId() {
        return applicationId;
    }

    public void setApplicationId(String applicationId) {
        this.applicationId = applicationId;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public Long getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(Long expireTime) {
        this.expireTime = expireTime;
    }

    public boolean equals(Object obj) {
        if (obj instanceof Token) {
            Token objToken = (Token)obj;
            if (objToken.getTokenKey().equals(this.getTokenKey())) {
                return true;
            }
        }

        return false;
    }

    public int hashCode() {
        return this.getTokenKey().hashCode();
    }

    public static String genUUID() {
        StringBuffer sb = new StringBuffer(40);
        sb.append(System.currentTimeMillis());
        sb.append("-");
        sb.append(random.nextLong());
        return sb.toString();
    }

    public boolean expired() {
        long millisecond = this.getExpireTime();
        long currentMs = System.currentTimeMillis();
        return millisecond < currentMs;
    }
}
